//
//  ZHNewFeatureViewController.swift
//  weibosina
//
//  Created by zhangzhen on 16/6/17.
//  Copyright © 2016年 zhangzhen. All rights reserved.
//

import UIKit
import SnapKit

private let ZHPageCount:CGFloat = 4
private let reuseIdentifier = "newfeature_Cell"

class ZHNewFeatureViewController: UICollectionViewController {
    
    //重写init方法来设置布局对象
    init(){
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

    override func viewDidLoad() {
        super.viewDidLoad()

       
        self.collectionView!.registerClass(NewFeatureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        //设置colectionview的属性
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        
    }
    
    

   

    

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return Int(ZHPageCount)
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureViewCell
        cell.imageIndex = indexPath.item
    
        // Configure the cell
    
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //获取偏移量
        let offsetX = scrollView.contentOffset.x
        if offsetX == (ZHPageCount-1)*scrollView.bounds.size.width{
            //获取当前的cell
            (collectionView?.visibleCells().last as? NewFeatureViewCell)?.showBtn()
        }
    }
    

 

}


// MARK: - 新特性 Cell
private class NewFeatureViewCell: UICollectionViewCell {
    //提供一个方法来展示动画
    private func showBtn(){
        btn.hidden = false
        
        //设置显示出来的时候是一个动画
        btn.transform = CGAffineTransformMakeScale(0, 0)
        btn.userInteractionEnabled = false
        
        UIView.animateWithDuration(1.2,         // 动画时长
            delay: 0,
            usingSpringWithDamping: 0.8,        // 弹力系数
            initialSpringVelocity: 10,          // 初始速度
            options: [],                        // 动画选项
            animations: {
                
                self.btn.transform = CGAffineTransformIdentity
                
        }) { (_)  in
            
            self.btn.userInteractionEnabled = true
        }

    }
    //图片的名字属性,根据当前cell的索引来获取的
    var imageIndex: Int = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex+1)")
            //使得所有的btn隐藏
            btn.hidden = true
        }
    }
    
    //子控件
    private lazy var iconView = UIImageView()
    //添加button
    private lazy var btn = UIButton(imageName: "new_feature_finish_button", titleName: "开始体验", color: UIColor.whiteColor())
    
    //重写init方法,自定义cell,并添加控件
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(iconView)
        addSubview(btn)
        iconView.frame = bounds
        
        //设置button
        btn.layer.cornerRadius = 5
        btn.layer.masksToBounds = true
        btn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(snp_centerX)
            make.bottom.equalTo(snp_bottom).multipliedBy(0.7)
        btn.addTarget(self, action: "didClickStartLoadButton", forControlEvents: .TouchUpInside)
              
        }
    
    }
    //点击了开始体验按钮
    @objc private func didClickStartLoadButton() {
        //切换主控制器
        
        UIApplication.sharedApplication().keyWindow?.rootViewController = ZHMainController()
        
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
  }

}
